dnl $Id$
dnl config.m4 for extension tclip

dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary. This file will not work
dnl without editing.

dnl If your extension references something external, use with:

PHP_ARG_WITH(tclip, for tclip support,
dnl Make sure that the comment is aligned:
[  --with-tclip             Include tclip support])

dnl Otherwise use enable:

dnl PHP_ARG_ENABLE(tclip, whether to enable tclip support,
dnl Make sure that the comment is aligned:
dnl [  --enable-tclip           Enable tclip support])

if test "$PHP_TCLIP" != "no"; then
  dnl Write more examples of tests here...

  dnl # get library FOO build options from pkg-config output
  dnl AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
  dnl AC_MSG_CHECKING(for libfoo)
  dnl if test -x "$PKG_CONFIG" && $PKG_CONFIG --exists foo; then
  dnl   if $PKG_CONFIG foo --atleast-version 1.2.3; then
  dnl     LIBFOO_CFLAGS=`$PKG_CONFIG foo --cflags`
  dnl     LIBFOO_LIBDIR=`$PKG_CONFIG foo --libs`
  dnl     LIBFOO_VERSON=`$PKG_CONFIG foo --modversion`
  dnl     AC_MSG_RESULT(from pkgconfig: version $LIBFOO_VERSON)
  dnl   else
  dnl     AC_MSG_ERROR(system libfoo is too old: version 1.2.3 required)
  dnl   fi
  dnl else
  dnl   AC_MSG_ERROR(pkg-config not found)
  dnl fi
  dnl PHP_EVAL_LIBLINE($LIBFOO_LIBDIR, TCLIP_SHARED_LIBADD)
  dnl PHP_EVAL_INCLINE($LIBFOO_CFLAGS)

  dnl # --with-tclip -> check with-path
  dnl SEARCH_PATH="/usr/local /usr"     # you might want to change this
  dnl SEARCH_FOR="/include/tclip.h"  # you most likely want to change this
  dnl if test -r $PHP_TCLIP/$SEARCH_FOR; then # path given as parameter
  dnl   TCLIP_DIR=$PHP_TCLIP
  dnl else # search default path list
  dnl   AC_MSG_CHECKING([for tclip files in default path])
  dnl   for i in $SEARCH_PATH ; do
  dnl     if test -r $i/$SEARCH_FOR; then
  dnl       TCLIP_DIR=$i
  dnl       AC_MSG_RESULT(found in $i)
  dnl     fi
  dnl   done
  dnl fi
  dnl
  dnl if test -z "$TCLIP_DIR"; then
  dnl   AC_MSG_RESULT([not found])
  dnl   AC_MSG_ERROR([Please reinstall the tclip distribution])
  dnl fi

  dnl # --with-tclip -> add include path
  dnl PHP_ADD_INCLUDE($TCLIP_DIR/include)

  dnl # --with-tclip -> check for lib and symbol presence
  dnl LIBNAME=tclip # you may want to change this
  dnl LIBSYMBOL=tclip # you most likely want to change this 

  dnl PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
  dnl [
  dnl   PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $TCLIP_DIR/$PHP_LIBDIR, TCLIP_SHARED_LIBADD)
  dnl   AC_DEFINE(HAVE_TCLIPLIB,1,[ ])
  dnl ],[
  dnl   AC_MSG_ERROR([wrong tclip lib version or lib not found])
  dnl ],[
  dnl   -L$TCLIP_DIR/$PHP_LIBDIR -lm
  dnl ])
  dnl
  dnl PHP_SUBST(TCLIP_SHARED_LIBADD)

  SEARCH_PATH="/usr/lib/pkgconfig/"     # you might want to change this
  SEARCH_FOR="opencv4.pc"  # you most likely want to change this
  if test -r $PHP_TCLIP/$SEARCH_FOR; then # path given as parameter
     TCLIP_DIR=$PHP_TCLIP
  else # search default path list
     AC_MSG_CHECKING([for opencv.pc file in default path])
     for i in $SEARCH_PATH ; do
       if test -r $i/$SEARCH_FOR; then
         TCLIP_DIR=$i
         AC_MSG_RESULT(found in $i)
       fi
     done
  fi
  dnl
  if test -z "$TCLIP_DIR"; then
     AC_MSG_RESULT([not found])
     AC_MSG_ERROR([does not find opencv.pc])
  fi

  OPENCV_FLAGS="`pkg-config opencv4 --libs --cflags opencv4`"
  opencv_lib_path=""
  for i in $OPENCV_FLAGS;do
        if test ${i:0:2} = "-I" ;then
                PHP_ADD_INCLUDE(${i:2})
        elif test ${i:0:2} = "-L" ;then
                opencv_lib_path=${i:2}
        elif test ${i:${#i}-3} = ".so" ;then
                dir_name=`dirname $i`
                file_name=${i/$dir_name/}
                file_name=${file_name/\/lib/}
                file_name=${file_name/.so/}
		        PHP_ADD_LIBRARY_WITH_PATH($file_name,$dir_name,TCLIP_SHARED_LIBADD)
        elif test ${i:${#i}-6} = ".dylib" ;then
                dir_name=`dirname $i`
                file_name=${i/$dir_name/}
                file_name=${file_name/\/lib/}
                file_name=${file_name/.dylib/}
		        PHP_ADD_LIBRARY_WITH_PATH($file_name,$dir_name,TCLIP_SHARED_LIBADD)
        elif test ${i:0:2} = "-l" ;then 
                file_name=${i:2}
                PHP_ADD_LIBRARY_WITH_PATH($file_name,$opencv_lib_path,TCLIP_SHARED_LIBADD)
	    else
		        AC_MSG_ERROR([no result from pkg-config opencv --libs --cflags opencv])
        fi
  done


  PHP_REQUIRE_CXX()
  PHP_SUBST(TCLIP_SHARED_LIBADD)
  PHP_ADD_LIBRARY(stdc++,1,TCLIP_SHARED_LIBADD)
  CXXFLAGS="$CXXFLAGS -std=c++11 "

  PHP_NEW_EXTENSION(tclip, tclip.cpp, $ext_shared)
fi
